/*
 * --------------------------------------------
 * 项目内工具函数集合，此页面尽量写注释
 * @version  1.0
 * @author   yuqijun(yuqijun@corp.netease.com)
 * --------------------------------------------
 */
define([
    'base/util'
], function(_ut) {

    var _ = {},
        noop = function(){};


    // 类型判断， 同typeof
    _.typeOf = function (o) {
        return o == null ? String(o) : ({}).toString.call(o).slice(8, -1).toLowerCase();
    }
    //获取不带参数的url路径
    _.getUrlPath = function(url){
        return url.split("?")?url.split("?")[0]:"";
    }
    _.findInList = function(id, list, ident){
        ident = ident || "id";
        var len = list.length;
        for(; len--;){
            if(list[len][ident] == id) return len
        }
        return -1;
    }
    _.redirect = function(url){
        var href="";
        if(window.from){
            href = url + ((url.indexOf("?")!=-1) ? "&":"?") +"from="+window.from;
        }else{
            href = url;
        }
        window.location.href= href;
    }
    _.merge = function(obj1, obj2){
        var
            type1 = _.typeOf(obj1),
            type2 = _.typeOf(obj2),
            len;

        if(type1 !== type2) return obj2;
        switch(type2){
            case 'object':
                for(var i in obj2){
                    if(obj2.hasOwnProperty(i)){
                        obj1[i] = _.merge(obj1[i], obj2[i]);
                    }
                }
                break;
            case "array":
                for(var i = 0, len = obj2.length; i < len; i++ ){
                    obj1[i] = _.merge(obj1[i], obj2[i]);
                }
                obj1.length = obj2.length;
                break;
            default:
                return obj2;
        }
        return obj1;
    }  // meregeList
    /**
     * list merge原列表
     * list2 新列表
     * 最后改动的list
     */
    _.mergeList = function(list, list2, ident){
        ident = ident || "id";
        var len = list.length;
        for(; len--;){
            for(var i = 0, len1 = list2.length; i < len1; i++){
                if(list2[i][ident] != null&&list2[i][ident] === list[len][ident]){
                    list.splice(len, 1, _.merge(list2[i],list[len]));
                    break;
                }
            }
        }
    }
    // 深度clone
    _.clone = function(obj){
        var type = _.typeOf(obj);
        switch(type){
            case "object":
                var cloned = {};
                for(var i in obj){
                    cloned[i] = _.clone(obj[i])
                }
                return cloned;
            case 'array':
                return obj.map(_.clone);
            default:
                return obj;
        }
        return obj;
    }

    _.extend = function(o1, o2 ,override){
        for( var i in o2 ) if( o1[i] == undefined || override){
            o1[i] = o2[i]
        }
        return o1;
    }

    _.copyObject = function(obj){
        var result;
        if(_ut._$isArray(obj,'array')){
            result =[];
            for(var i=0,l=obj.length;i<l;i++){
                if(typeof obj[i] =='object'){
                    result.push(_.copyObject(obj[i]));
                } else{
                    result.push(obj[i]);
                }
            }
        } else if(_ut._$isObject(obj,'array')){
            result ={};
            for(var i in obj){
                if(obj.hasOwnProperty(i)){
                    if(typeof obj[i]=='object'){
                        result[i] = _.copyObject(obj[i]);
                    } else{
                        result[i] = obj[i];
                    }
                }
            }
        }
        return result;
    };
    /**
     * 压缩regular模版
     * @param htmlstr
     * @returns {XML|string}
     * add by xuejimiao 2016/02/25
     */
    _.compressHtml = function(htmlstr){
        //防止nej打包模版后报错
        if(typeof htmlstr !== "string"){
            return htmlstr;
        }
        return htmlstr.replace(/[\r\n]|\s+(?=[<{])/g,'').replace(/[}>]\s+/g, function(value){
            return value.substr(0,1);
        })
    };
    _.filterNoneData = function(_data){
        for(var _key in _data){

            if(_data.hasOwnProperty(_key)){
                if(typeof _data[_key] == 'number' && isNaN(_data[_key])){
                    delete _data[_key];
                }
            }
        }
    }

    _.filterParam = function(_data) {
        var obj = JSON.parse(JSON.stringify(_data));
        for ( var key in obj ) {
            if ( obj.hasOwnProperty(key) ) {
                if ( !obj[key] ) {
                    delete obj[key];
                }
            }
        }
        return obj;
    }

    _.numRule = [{
        method:function(value) {
            var reg = /^\d*$/;
            return !value || reg.test(value);
        },
        'message':'请输入整数'
    }];

    _.floatRule = function(len) {
        if ( len == 2 ) {
            return [{
                method:function(value) {
                    var reg = /^\d+(\.(\d){0,2})?$/;
                    return !value || reg.test(value);
                },
                'message':'最多两位小数'
            }];
        } else if ( len == 4 ) {
            return [{
                method:function(value) {
                    var reg = /^\d+(\.(\d){0,4})?$/;
                    return !value || reg.test(value);
                },
                'message':'最多四位小数'
            }];
        } else {
            return [{
                method:function() { return true; }
            }]
        }
    };
    /**
     * 获取url中的参数值
     */
    _.getUrlParam = function( name, url ) {
        if (!url) url = location.href;
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( url );
        return results == null ? null : results[1];
    }

    return _;

});
